package com.joint.web.action.app;

import com.fz.us.base.bean.BaseEnum;
import com.fz.us.base.bean.Pager;
import com.google.common.collect.Maps;
import com.joint.base.entity.Users;
import com.joint.base.service.UsersService;
import com.joint.core.entity.Announce;
import com.joint.core.service.AnnounceService;
import com.joint.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台Action类 - 公告管理
 */
@ParentPackage("app")
public class AnnounceAction extends BaseAction implements ModelDriven<Announce> {

    private static final long serialVersionUID = -3957546401899874034L;

    private Announce announce;
	private List<Announce> announceList;

    @Resource
    private UsersService usersService;
	@Resource
	private AnnounceService announceService;

	/**
	 * 公告列表
	 * @return String
	 * @throws Exception
	 */
	public String getList() throws Exception {

        pager = new Pager();
        pager.setPageNumber(pageIndex);// 页码
        pager.setPageSize(pageSize);// 页Size
        if(StringUtils.isNotEmpty(pageProperty)&&StringUtils.isNotEmpty(pageKeyword)){
            pager.setProperty(pageProperty);
            pager.setKeyword(pageKeyword);
        }
        pager.setOrderBy("publishTime");
        pager.setOrderType(BaseEnum.OrderType.desc);

        Map<String, Object> map;
        map = Maps.newHashMap();
        map.put("name", pageKeyword);
        map.put("company", usersService.getCompanyByUser());
        map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});
        pager = announceService.findByPager(pager,map);
//        pager = announceService.findByPagerAndCompany(pager,null,usersService.getCompanyByUser(),map);

        announceList = (List<Announce>) pager.getList();
        if(announceList == null){
            announceList = new ArrayList<Announce>();
        }

        List<JSONObject> dataRows = new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();

        for (Announce temp : announceList) {
            Map<String, Object> tempMap = announceService.getListItemMap(temp);
            dataRows.add(JSONObject.fromObject(tempMap));
        }

        data.put("pageNum", pageIndex);//页数
        data.put("pageSize", announceList.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("dataRows", JSONArray.fromObject(dataRows));//数据

        return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
	}

    /**
     * 和用户相关的公告列表
     * @return String
     * @throws Exception
     */
    public String getListRelated() throws Exception {

        pager = new Pager();
        pager.setPageNumber(pageIndex);// 页码
        pager.setPageSize(pageSize);// 页Size
        if(StringUtils.isNotEmpty(pageProperty)&&StringUtils.isNotEmpty(pageKeyword)){
            pager.setProperty(pageProperty);
            pager.setKeyword(pageKeyword);
        }
        pager.setOrderBy("publishTime");
        pager.setOrderType(BaseEnum.OrderType.desc);

        Map<String, Object> map;
        map = Maps.newHashMap();
        map.put("name", pageKeyword);
        map.put("company", usersService.getCompanyByUser());
        map.put("state", new BaseEnum.StateEnum[]{BaseEnum.StateEnum.Enable});

        Users user = getRequestUsers();
        DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Announce.class);
        detachedCriteria.add(Restrictions.or(
                Restrictions.eq("callType", 0),
                Restrictions.sqlRestriction("this_.id in(select id from ec_announce_users as eau where eau.userId='" + user.getId() + "')")
        ));

        pager = announceService.findByPager(pager,detachedCriteria,null,null,null,null,map);

        announceList = (List<Announce>) pager.getList();
        if(announceList == null){
            announceList = new ArrayList<Announce>();
        }

        List<JSONObject> dataRows = new ArrayList<JSONObject>();
        Map<String, Object> data = new HashMap<String, Object>();

        for (Announce temp : announceList) {
            Map<String, Object> tempMap = announceService.getListItemMap(temp);
            dataRows.add(JSONObject.fromObject(tempMap));
        }

        data.put("pageNum", pageIndex);//页数
        data.put("pageSize", announceList.size());//记录数
        data.put("pageCount", pager.getPageCount());//总页数
        data.put("totalCount", pager.getTotalCount());//总记录数
        data.put("dataRows", JSONArray.fromObject(dataRows));//数据

        return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
    }

	/**
	 * 公告信息详情
     *
	 * @return String
	 * @throws Exception
	 */
	public String getDetail() throws Exception {

        if (StringUtils.isNotEmpty(keyId)) {
            announce = announceService.get(keyId);
            if (announce != null) {
                Map<String, Object> data = announceService.getDetailMap(announce);
                return ajaxHtmlAppResult(1, "", JSONObject.fromObject(data));
            }
        }
        return ajaxHtmlAppResult(0, "无法找到该文档，可能已被删除！", null);
	}

	@Override
	public Announce getModel() {
		if(announce == null){
            announce = new Announce();
		}
		return announce;
	}

}
